/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MessageDialog.java
 *
 * Created on Oct 10, 2011, 6:37:18 PM
 */
package AMP2.MainDisplay;

import AMP2.Util.MiscStuff;

/**
 *
 * @author awittig
 */
public class MessageDialog extends javax.swing.JFrame {
    
    //for integration tests:
    public static MessageDialog _integrationTesting_lastInstance;
    
    private enum Mode{
        FATAL, WARN
    }
    
    private final Mode mode;

    
    public static MessageDialog getNewMessageDialog(final Mode mode, final Exception e){
        final MessageDialog md = new MessageDialog(mode, e);
        
        _integrationTesting_lastInstance = md;
        
        md.init(e);
        md.setVisible(true);
        return md;
    }
    
    public static MessageDialog FatalMessageDialog(final Exception e){
        return getNewMessageDialog(Mode.FATAL, e);
    }
    
    public static MessageDialog WarnMessageDialog(final Exception e){
        return getNewMessageDialog(Mode.WARN, e);
    }
    
    private MessageDialog(final Mode mode, final Exception e) {
        initComponents();
        this.mode = mode;
        
        //log the error
        MiscStuff.writeToLog(e);
        
        //display the info
        logFileLocation.setText(MiscStuff.getNameOfFile());
        errorTxt.setText(MiscStuff.getStackTrace(e));
        
        helpfulTxt.setText("Email Anthony and tell him to add a helpful message!");
        final StringBuilder userReadableMessages = new StringBuilder();
        Throwable current = e;
        while(current != null){
            if(current instanceof UserReadableMessage){
                final UserReadableMessage urm = (UserReadableMessage) current;
                final String message = urm.getUserReadableMessage();
                if(message != null){
                    userReadableMessages.append(message).append(System.getProperty("line.separator"));
                }
            }
            current = current.getCause();
        }
        if(0 < userReadableMessages.length()){
            helpfulTxt.setText(userReadableMessages.toString());
        }
        
    }
    
    private void init(final Exception e){
        
        switch(mode){
            case FATAL:
                header.setText("Something really bad happened... email Anthony the log file at:");
                exitText.setText("You should probably exit the program (this button and the red x will not give you the option to save):");
                closeButton.setText("Exit Program");
                break;
            case WARN:
                header.setText("Something kind of bad happened. You can probably continue on your way. The log file is at:");
                exitText.setText("");
                closeButton.setText("ok");
                break;
            default:
                FatalMessageDialog(new Exception("couldn't create message dialog for mode: " + mode + 
                        ", while trying to display a message for the underlying exception"));
        }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        errorTxt = new javax.swing.JTextArea();
        closeButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        logFileLocation = new javax.swing.JTextArea();
        header = new javax.swing.JLabel();
        exitText = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        helpfulMessage = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        helpfulTxt = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        errorTxt.setColumns(20);
        errorTxt.setRows(5);
        jScrollPane1.setViewportView(errorTxt);

        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        logFileLocation.setColumns(20);
        logFileLocation.setLineWrap(true);
        logFileLocation.setRows(5);
        jScrollPane2.setViewportView(logFileLocation);

        exitText.setText("exitTxt");

        jLabel3.setText("This is part of the log file, it shouldn't mean anything to you:");

        helpfulMessage.setText("This might be a helpful message:");

        helpfulTxt.setColumns(20);
        helpfulTxt.setLineWrap(true);
        helpfulTxt.setRows(5);
        helpfulTxt.setWrapStyleWord(true);
        jScrollPane3.setViewportView(helpfulTxt);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane2)
                        .add(18, 18, 18))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane3)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(exitText)
                            .add(header)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 896, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(helpfulMessage)
                            .add(jLabel3)
                            .add(closeButton))
                        .add(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(9, 9, 9)
                .add(header, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(25, 25, 25)
                .add(helpfulMessage)
                .add(4, 4, 4)
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 136, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 136, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(exitText)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(closeButton)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        switch(mode){
            case FATAL:
                System.exit(1);
                break;
            case WARN:
                this.setVisible(false);
                break;
            default:
                FatalMessageDialog(new Exception("couldn't close message dialog for mode: " + mode + 
                        ", while trying to close a message for the underlying exception"));
        }
    }//GEN-LAST:event_closeButtonActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JTextArea errorTxt;
    private javax.swing.JLabel exitText;
    private javax.swing.JLabel header;
    private javax.swing.JLabel helpfulMessage;
    private javax.swing.JTextArea helpfulTxt;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea logFileLocation;
    // End of variables declaration//GEN-END:variables
}
